#!/bin/bash
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#

# Script for updating Authentication Configuration.

function printUsage {

  echo 
  echo "Usage: $script_name"
  echo "       Script for updating Authentication Configuration"
  echo
}

# ===================================================================
# start of MAIN
# ===================================================================

script_name=`/bin/basename $0`

while [ $# -gt 0 ]; do
  case $1 in

    -h | --help)
      printUsage
      exit 2
      ;;

    **)
      echo ""
      echo "ERROR:  Invalid option: $1 "
      printUsage
      exit 2
      ;;

  esac
  shift
done

if [  -z $TRAF_VAR ]; then
  echo
  echo "The TRAF_VAR environment variable does not exist."
  echo "Please ensure sqenv.sh has been sourced, and re-run $script_name."
  echo
  exit 1;
fi

if [ -z "$CACERTS_DIR" ]; then
  CACERTS_DIR=$HOME/cacerts
fi

# Creating folders for storing CA certificates
echo "Creating folders for storing certificates"
if [ ! -d "$CACERTS_DIR" ]; then
  mkdir -p -m 700 $CACERTS_DIR;
else
  chmod 700 $CACERTS_DIR
fi

#Create local folder for storing CA certicates
SQCACERTS_DIR=$TRAF_VAR/cacerts
if [ ! -d "$SQCACERTS_DIR" ]; then
  mkdir -p -m 700 $SQCACERTS_DIR;
else
  chmod 700 $SQCACERTS_DIR
fi

#Create local folder for storing SQ certicates
SQCERT_DIR=$TRAF_VAR/sqcert
if [ ! -d "$SQCERT_DIR" ]; then
  mkdir -p -m 700 $SQCERT_DIR;
else
  chmod 700 $SQCERT_DIR
fi

#cp -p -r $CACERTS_DIR $TRAF_HOME 2> /dev/null

#if [ -n "$CLUSTERNAME" ]; then  
#  $SQ_PDCP -p -r $MY_NODES -x `uname -n` $CACERTS_DIR $TRAF_HOME 2> /dev/null
#
#  cp -p -r $HOME/sqcert $TRAF_HOME 2> /dev/null
#  $SQ_PDCP -p -r $MY_NODES -x `uname -n` $HOME/sqcert $TRAF_HOME 2> /dev/null
#
#fi
exit 0;

